package cn.com.sbell.vss;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import cn.com.sbell.vss.VissSession;

public class VissSession extends BaseObject {
	private String sessionId;

	private String userId;

	private String username;

	private String displayName;

	private volatile long lastAccessTime;

	private long loginTime;

	// private Map areaRoleMap;

	private Map domainAreaRoleMap = new HashMap();

	private String ip;

	private volatile int alarmPort;

	private volatile int sendAlarmRetries;

	private String organizationId;

	private String domainId;

	private String vapPath;

	private String currentVsIp;

	private int currentVsPort;
	
	private String currentVsDeviceId;

	private String currentVsLoginUsername;

	private String currentVsLoginPassword;
//
	private String nextHopDomainUsername;
	private String nextHopDomainPassword;
//	all sessionId belongs to different next hop CSG will be stored here
	private HashMap nextHopSessionIdMap=new HashMap();
	
	
	synchronized public void invalidate() {
		this.alarmPort = -1;
		if (domainAreaRoleMap != null) {
			this.domainAreaRoleMap.clear();
		}
		this.lastAccessTime = -1;
	}

	public VissSession(String sessionId) {
		this.setSessionId(sessionId);
	}

	// public String getRoleId(String areaId) {
	// if (getAreaRoleMap() == null)
	// return null;
	// return (String) getAreaRoleMap().get(areaId);
	// }
	//
	// public void addAreaRole(String areaId, String roleId) {
	// if (getAreaRoleMap() == null)
	// setAreaRoleMap(new HashMap());
	// getAreaRoleMap().put(areaId, roleId);
	// }

	public Set getAreaIds(String domainId) {
		Map map = getAreaRoleMap(domainId);
		if (map == null) {
			return Collections.EMPTY_SET;
		}
		return map.keySet();
	}

	/**
	 * 
	 * @param domainId
	 * @param roleIds
	 * @return return allowed areaIds, never return null;
	 */
	public List getAreaIds(String domainId, List roleIds) {
		Map map = getAreaRoleMap(domainId);
		if (map == null) {
			return Collections.EMPTY_LIST;
		}
		List areaIds = new ArrayList();
		for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
			Entry entry = (Entry) iter.next();
			if (roleIds.contains(entry.getValue())) {
				areaIds.add(entry.getKey());
			}
		}
		return areaIds;
	}

	public String getDisplayName() {
		return displayName;
	}

	public long getLastAccessTime() {
		return lastAccessTime;
	}

	public Map getAreaRoleMap(String domainId) {
		return (Map) domainAreaRoleMap.get(domainId);
	}

	public String getSessionId() {
		return sessionId;
	}

	public String getUsername() {
		return username;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public void setLastAccessTime(long lastAccessTime) {
		this.lastAccessTime = lastAccessTime;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public long getLoginTime() {
		return loginTime;
	}

	public void setLoginTime(long loginTime) {
		this.loginTime = loginTime;
	}

	public void setAreaRoleMap(String domainId, Map roleIdMap) {
		domainAreaRoleMap.put(domainId, roleIdMap);
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getAlarmPort() {
		return alarmPort;
	}

	public void setAlarmPort(int alarmPort) {
		this.alarmPort = alarmPort;
	}

	public void setSendAlarmRetries(int sendAlarmRetries) {
		this.sendAlarmRetries = sendAlarmRetries;
	}

	public int getSendAlarmRetries() {
		return this.sendAlarmRetries;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public String getVapPath() {
		return vapPath;
	}

	public void setVapPath(String vapPath) {
		this.vapPath = vapPath;
	}

	public String getDomainId() {
		return domainId;
	}

	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}

	public String getCurrentVsIp() {
		return currentVsIp;
	}

	public void setCurrentVsIp(String currentVsIp) {
		this.currentVsIp = currentVsIp;
	}

	public String getCurrentVsLoginPassword() {
		return currentVsLoginPassword;
	}

	public void setCurrentVsLoginPassword(String currentVsLoginPassword) {
		this.currentVsLoginPassword = currentVsLoginPassword;
	}

	public String getCurrentVsLoginUsername() {
		return currentVsLoginUsername;
	}

	public void setCurrentVsLoginUsername(String currentVsLoginUsername) {
		this.currentVsLoginUsername = currentVsLoginUsername;
	}

	public int getCurrentVsPort() {
		return currentVsPort;
	}

	public void setCurrentVsPort(int currentVsPort) {
		this.currentVsPort = currentVsPort;
	}

	/**
	 * @return the currentVsDeviceId
	 */
	public String getCurrentVsDeviceId() {
		return currentVsDeviceId;
	}

	/**
	 * @param currentVsDeviceId the currentVsDeviceId to set
	 */
	public void setCurrentVsDeviceId(String currentVsDeviceId) {
		this.currentVsDeviceId = currentVsDeviceId;
	}

	public String getNextHopDomainPassword() {
		return nextHopDomainPassword;
	}

	public void setNextHopDomainPassword(String nextHopDomainPassword) {
		this.nextHopDomainPassword = nextHopDomainPassword;
	}

	public String getNextHopDomainUsername() {
		return nextHopDomainUsername;
	}

	public void setNextHopDomainUsername(String nextHopDomainUsername) {
		this.nextHopDomainUsername = nextHopDomainUsername;
	}

	public String getNextHopSessionId(String route)
	{
		return (String)nextHopSessionIdMap.get(route);
	}
	public void putNextHopSessionId(String route,String sessionId)
	{
		nextHopSessionIdMap.put(route, sessionId);
	}
	public void removeNextHopSessionId(String route)
	{
		nextHopSessionIdMap.remove(route);
	}
}
